var type = RequestURLParam.getParam("type");
var id = RequestURLParam.getParam("id");
var minForm;
var user = window.top.UserInfo;
var urlTxt = "sysEntrust!add.do";
$(document).ready(function () {
    minForm = new mini.Form("form");
    AgentEdit = mini.get("AgentEdit");
    if (type == "edit") {
        urlTxt = "sysEntrust!update.do";
        init_data();
    } else {
        mini.get("entrustConsigner").setValue(user.userName);
        mini.get("entrustConsignerId").setValue(user.userId);
    }
});

function init_data() {
    ActHttpRequest("sysEntrust!findById.do?id=" + id, {}, function (data) {
        if (data.OK == "OK") {
            if (data.sysEntrust) {
                $("#form .sysEntrust").each(function () {
                    mini.get(this.id).setValue(data.sysEntrust[this.id]);
                });
                /*************添加非mini组件的初始化或mini中value/Text等组合组件的初始化 start*************************/
                AgentEdit.setValue(data.sysEntrust["entrustAgentId"]);
                AgentEdit.setText(data.sysEntrust["entrustAgent"]);

                /*************添加非mini组件的初始化或mini中value/Text等组合组件的初始化 end*************************/
            }
        } else {
            mini.alert(data.message);
        }
    }, null, null, null, '加载中。。。');
}

function saveData() {
    minForm.validate();
    if (minForm.isValid() == false) {
        mini.alert("验证失败，点击页面红色叹号查看！");
        return;
    }
    var formData = $("#form").serializeJSON();
    ActHttpRequest(urlTxt, formData, function (data) {
        if (data.OK == "OK") {
            window.CloseOwnerWindow("OK");
        } else {
            mini.alert(data.message);
        }
    }, null, null, null, '保存中。。。', true);
}

function selPerson() {
    mini.open({
        url: "base/dialog/selPerson.html?type=org&orgid=" + user.ognId + "&MultiSelect=true&temp=" + new Date().getUTCMilliseconds(),
        title: "选择委托的人员",
        width: 720,
        height: 450,
        onload: function () {
        },
        ondestroy: function (e) {
            if (e != 'close' && e.id) {
                if (e.id.indexOf(user.userId)>-1) {
                    mini.alert("请勿选择自己作为代理人");
                } else {
                    AgentEdit.setValue(e.id);
                    AgentEdit.setText(e.name);
                    mini.get('entrustAgent').setValue(e.name);
                    mini.get('entrustAgentId').setValue(e.id);
                }
            }
        }
    });
}

function clearPerson() {
    mini.get('entrustAgent').setValue("");
    mini.get('entrustAgentId').setValue("");
    AgentEdit.setValue("");
    AgentEdit.setText("");
}
